<?php

namespace Ecmpay\Ecmpay;

class Ecmpay extends Base
{
    public static function init($userConfig)
    {
        parent::init($userConfig);
    }
    /**
     * PC支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4685709434024713
     */
    public static function alipayForPc(array $orderData, string $return_url, string $notify_url = null)
    {
        return self::$alipay::alipayForPc($orderData, $return_url, $notify_url);
    }
    /**
     * H5支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689273557058296
     */
    public static function alipayForWap(array $orderData, string $return_url, string $notify_url = null)
    {
        return self::$alipay::alipayForWap($orderData, $return_url, $notify_url);
    }
    /**
     * 用户扫商家码支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689284856533264
     */
    public static function alipayForQrcode(array $orderData, string $return_url, string $notify_url = null)
    {
        return self::$alipay::alipayForQrcode($orderData, $return_url, $notify_url);
    }
    /**
     * 商家通过扫描用户出示的付款码进行支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689288176316250
     */
    public static function alipayForPaymentCode(array $orderData, string $notify_url = null)
    {
        return self::$alipay::alipayForPaymentCode($orderData, $notify_url);
    }
    /**
     * 支付宝关闭支付订单
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699193402878998
     */
    public static function alipayTradeClose(array $orderData, string $notify_url = null)
    {
        return self::$alipay::alipayTradeClose($orderData, $notify_url);
    }
    /**
     * App支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-05-07 12:07:27
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-05-07 12:07:27
     */
    public static function alipayForApp(array $orderData, string $notify_url = null)
    {
        return self::$alipay::alipayForApp($orderData, $notify_url);
    }
    /**
     * 微信Native支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699165483036275
     */
    public static function wxpayForNative(array $orderData, string $notify_url = null)
    {
        return self::$wxpay::wxpayForNative($orderData, $notify_url);
    }
    /**
     * 微信JSAPI支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699165483036275
     */
    public static function wxpayForJsApi(array $orderData, string $notify_url = null)
    {
        return self::$wxpay::wxpayForJsApi($orderData, $notify_url);
    }
    /**
     * 微信APP支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699138781635476
     */
    public static function wxpayForApp(array $orderData, string $notify_url = null)
    {
        return self::$wxpay::wxpayForApp($orderData, $notify_url);
    }
    /**
     * 微信H5支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699162923109473
     */
    public static function wxpayForH5(array $orderData, string $notify_url = null)
    {
        return self::$wxpay::wxpayForH5($orderData, $notify_url);
    }
    /**
     * 付款码支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699167841791876
     */
    public static function wxpayForPaymentCode(array $orderData, string $notify_url = null)
    {
        return self::$wxpay::wxpayForPaymentCode($orderData, $notify_url);
    }
    /**
     * 支付宝/微信退款
     * @param array $orderData
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-05-07 12:07:27
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-05-07 12:07:27
     */
    public static function refund(array $orderData, $payType)
    {
        if (isset($orderData['mch_no']) && !empty($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        } else {
            $postData['sub_mchid'] = $orderData['mch_id'];
        }
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['refund_amount']) || empty($orderData['refund_amount'])) {
            throw new \Exception("缺少退款金额: refund_amount");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少总金额: total_amount");
        }
        if (!isset($orderData['out_request_no']) || empty($orderData['out_request_no'])) {
            throw new \Exception("缺少退款单号: out_request_no");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if ($payType == 'alipay' && (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id']))) {
            throw new \Exception("初始化配置中缺少授权的支付宝应用APPID: app_id");
        }
        switch ($payType) {
            case 'alipay':
                $apiUrl = self::$ecmUrl . 'alipay/refund';
                break;
            case 'wxpay':
                $apiUrl = self::$ecmUrl . 'wxpay/refund';
                break;
            default:
                # code...
                break;
        }
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['out_request_no'] = $orderData['out_request_no'];
        $postData['refund_amount'] = $orderData['refund_amount'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['sign'] = self::$lib::createSign($postData, self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $resultArr = json_decode($result['data'], true);
        if (empty($resultArr) || !isset($resultArr['code'])) {
            throw new \Exception($resultArr['msg']);
        } else {
            return $resultArr;
        }
    }
    /**
     * 支付宝/微信转帐
     * @param array $orderData
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-05-07 12:07:27
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-05-07 12:07:27
     */
    public static function transfer(array $orderData, $payType)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['trans_amount']) || empty($orderData['trans_amount'])) {
            throw new \Exception("缺少退款金额: trans_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['trans_amount'] = $orderData['trans_amount'];
        $postData['subject'] = $orderData['subject'];
        switch ($payType) {
            case 'alipay':
                if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
                    throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
                }
                if (!isset($orderData['identity']) || empty($orderData['identity'])) {
                    throw new \Exception("缺少参与方的标识 ID: identity");
                }
                $postData['identity'] = $orderData['identity'];
                if (!isset($orderData['name']) || empty($orderData['name'])) {
                    throw new \Exception("缺少参与方真实姓名: name");
                }
                $postData['name'] = $orderData['name'];
                if (!isset($orderData['payer_show_name']) || empty($orderData['payer_show_name'])) {
                    throw new \Exception("缺少转帐来源账户名: payer_show_name");
                }
                if (!isset($orderData['identity_type']) || empty($orderData['identity_type'])) {
                    throw new \Exception("缺少参与方的标识类型: identity_type");
                }
                $postData['payer_show_name'] = $orderData['payer_show_name'];
                $postData['identity_type'] = $orderData['identity_type'];
                $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
                return self::$alipay::transferAlipay($postData);
                break;
            case 'wxpay':
                if (!isset($orderData['mch_no']) || empty($orderData['mch_no'])) {
                    throw new \Exception("缺少普通商户的调用商户号，为WXM开头的字符串: mch_no");
                }
                if (!isset($orderData['check_name']) || empty($orderData['check_name'])) {
                    throw new \Exception("缺少校验用户姓名选项: check_name");
                }
                if (!isset($orderData['client_ip']) || empty($orderData['client_ip'])) {
                    throw new \Exception("缺少IP地址: client_ip");
                }
                if (!isset($orderData['openid']) || empty($orderData['openid'])) {
                    throw new \Exception("缺少用户opneid: openid");
                }
                $postData['mch_no'] = $orderData['mch_no'];
                $postData['check_name'] = $orderData['check_name'];
                $postData['client_ip'] = $orderData['client_ip'];
                $postData['openid'] = $orderData['openid'];
                return self::$wxpay::mchTransfers($postData);
                break;
            default:
                # code...
                break;
        }
    }
    /**
     * 签名校验
     * @param [type] $data
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2022-03-16 14:46:16
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2022-03-16 14:46:16
     */
    public static function signVerify($data)
    {
        return $data['sign'] === self::$lib::createSign($data, self::$config['appSecret']);
    }
}
